package org.zwy.gradetrack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.zwy.gradetrack.entity.DirectEvaluation;
import org.zwy.gradetrack.mapper.DirectEvaluationMapper;
import org.zwy.gradetrack.mapper.CourseScoreMapper;
import org.zwy.gradetrack.mapper.CourseGoalAchievementMapper;
import org.zwy.gradetrack.mapper.CourseSupportMatrixMapper;
import org.zwy.gradetrack.service.DirectEvaluationService;
import org.zwy.gradetrack.vo.DirectEvaluationVO;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 直接评价服务实现类
 */
@Service
public class DirectEvaluationServiceImpl extends ServiceImpl<DirectEvaluationMapper, DirectEvaluation> implements DirectEvaluationService {

    @Autowired
    private CourseScoreMapper courseScoreMapper;

    @Autowired
    private CourseGoalAchievementMapper courseGoalAchievementMapper;

    @Autowired
    private CourseSupportMatrixMapper courseSupportMatrixMapper;

    @Override
    public Page<DirectEvaluationVO> pageWithCondition(Page<DirectEvaluation> page, DirectEvaluation evaluation) {
        Page<DirectEvaluationVO> voPage = new Page<>(page.getCurrent(), page.getSize());
        List<DirectEvaluationVO> records = baseMapper.pageDirectEvaluationsWithDetails(voPage, evaluation);
        voPage.setRecords(records);
        voPage.setTotal(page.getTotal()); // Maintain total from original page
        return voPage;
    }

    @Override
    @Transactional
    public void calculateEvaluationMetrics(DirectEvaluation evaluation) {
        // 1. 计算参与评价学生数
        Integer studentCount = courseScoreMapper.countDistinctStudentsByCourse(evaluation.getCourseId());
        evaluation.setStudentCount(studentCount);

        // 2. 计算课程平均分
        BigDecimal avgScore = courseScoreMapper.calculateAverageScore(evaluation.getCourseId());
        evaluation.setAvgScore(avgScore);

        // 3. 计算达标率
        if (studentCount > 0) {
            Integer passCount = courseScoreMapper.countPassingStudents(evaluation.getCourseId());
            BigDecimal passRate = new BigDecimal(passCount)
                .multiply(new BigDecimal("100"))
                .divide(new BigDecimal(studentCount), 2, RoundingMode.HALF_UP);
            evaluation.setPassRate(passRate);
        }

        // 4. 获取课程目标达成度
        BigDecimal goalAchievement = courseGoalAchievementMapper.getGoalAchievement(
            evaluation.getCourseId()
        );
        evaluation.setGoalAchievement(goalAchievement);

        // 5. 获取支撑权重
        BigDecimal supportWeight = courseSupportMatrixMapper.getSupportWeight(
            evaluation.getCourseId(),
            evaluation.getIndicatorId()
        );
        evaluation.setSupportWeight(supportWeight);

        // 6. 计算直接评价分数
        if (goalAchievement != null && supportWeight != null) {
            BigDecimal evaluationScore = goalAchievement.multiply(supportWeight)
                .setScale(4, RoundingMode.HALF_UP);
            evaluation.setEvaluationScore(evaluationScore);
        }
    }

    @Override
    public List<DirectEvaluation> getByCourseAndIndicator(Integer courseId, Integer indicatorId) {
        return baseMapper.selectByCourseAndIndicator(courseId, indicatorId);
    }

    @Override
    public BigDecimal getLatestEvaluationScore(Long indicatorId, Integer evaluationYear) {
        return baseMapper.selectLatestEvaluationScore(indicatorId, evaluationYear);
    }
} 